home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / VideoToolbox 94.11.17 / VideoToolboxSources / PrintfGWorld.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-17  |  2.9 KB  |  103 lines  |  [TEXT/KAHL]

  1. /*
  2. PrintfGWorld.c
  3.  
  4.     void PrintfGWorld(GWorldPtr our);
  5. Uses "printf" to print out the GWorld as a bitmap, representing blank as '.' and
  6. non-blank as '#'. The origin is designated '+'. Uses the upper lefthand pixel
  7. as the definition of blank (a poor but convenient assumption). This is very crude, 
  8. and intended only for debugging.
  9.  
  10.     void PrintStringAsBitmap(unsigned char *s);
  11. Uses DrawString to render the string in a GWorld, and then calls PrintfGWorld to
  12. render the GWorld onto the console. For debugging.
  13.  
  14. HISTORY:
  15. 1/17/94 dgp wrote it.
  16. 6/18/94 dgp Call SetGWorld(GetMainDevice())) before calling printf.
  17. 6/22/94 dgp Fixed bug: I was "restoring" to a GWorldPtr that was never initialized.
  18. 9/5/94 dgp removed assumption in printf's that int==short.
  19. */
  20. #include "VideoToolbox.h"
  21. void PrintfGWorld(GWorldPtr our);
  22. void PrintStringAsBitmap(unsigned char *s);
  23.  
  24. void PrintfGWorld(GWorldPtr our)
  25. // Print GWorld on the console as a bitmap.
  26. // Assume upper lefthand pixel is blank, which is rendered as '.'. 
  27. // Any other pixel value is represented as '#'.
  28. {
  29.     Rect r;
  30.     register unsigned long *pix,blank;
  31.     register int i,n;
  32.     int y;
  33.     char *bit;
  34.     GDHandle oldDevice;
  35.     long qD=0;
  36.   
  37.     Gestalt(gestaltQuickdrawVersion,&qD);
  38.     if(qD>=gestalt8BitQD){
  39.         oldDevice = GetGDevice();
  40.         SetGDevice(GetMainDevice());    // needed for printf
  41.     }
  42.     r=our->portRect;
  43.     r.left=0;
  44.     #if (THINK_C || THINK_CPLUS)
  45.         r.right=console_options.ncols;
  46.     #else
  47.         r.right=72;
  48.     #endif
  49.     CenterRectInRect(&r,&our->portRect);
  50.     SectRect(&r,&our->portRect,&r);
  51.     n=r.right-r.left;
  52.     bit=(char *)malloc((1+n)*sizeof(char));
  53.     if(bit==NULL)PrintfExit("PrintStringAsBitmap: Couldn't allocate %n bytes.\n",n+1);
  54.     bit[n]=0;
  55.     pix=(unsigned long *)malloc(n*sizeof(unsigned long));
  56.     if(pix==NULL)PrintfExit("PrintStringAsBitmap: Couldn't allocate %ld bytes.\n"
  57.         ,n*sizeof(long));
  58.     GetWindowPixelsQuickly((WindowPtr)our,our->portRect.left,our->portRect.top,pix,1);
  59.     blank=pix[0];
  60.     for(y=r.top;y<r.bottom;y++){
  61.         GetWindowPixelsQuickly((WindowPtr)our,r.left,y,pix,n);
  62.         for(i=0;i<n;i++){
  63.             if(pix[i]!=blank) bit[i]='#';                    // non-blank
  64.             else bit[i]='.';                                // blank
  65.         }
  66.         if(y==0)bit[-r.left]='+';                            // the origin
  67.         printf("%s\n",bit);
  68.     }
  69.     free(pix);
  70.     free(bit);
  71.     if(qD>=gestalt8BitQD)SetGDevice(oldDevice);
  72. }
  73.  
  74. void PrintStringAsBitmap(unsigned char *s)
  75. {
  76.     GWorldPtr our,old;
  77.     GDHandle oldDevice;
  78.     FontInfo f;
  79.     Rect r;
  80.     int error;
  81.  
  82.     // Draw string into a new GWorld
  83.     GetFontInfo(&f);
  84.     SetRect(&r,0,-f.ascent,StringWidth(s),f.descent);    // nominal size
  85.     InsetRect(&r,-(f.widMax/2+2),-(f.leading+2));        // add margin
  86.     error=NewGWorld(&our,1,&r,NULL,NULL,0);
  87.     if(error)PrintfExit("PrintStringAsBitmap: NewGWorld error %d.\n",error);
  88.     GetGWorld(&old,&oldDevice);
  89.     SetGWorld(our,NULL);
  90.     TextFace(old->txFace);
  91.     TextFont(old->txFont);
  92.     TextSize(old->txSize);
  93.     EraseRect(&our->portRect);
  94.     MoveTo(0,0);
  95.     DrawString(s);
  96.     SetGWorld(old,oldDevice);
  97.     
  98.     PrintfGWorld(our);
  99.     
  100.     DisposeGWorld(our);
  101. }
  102.  
  103.